package com.sojson.result.interfaces.impl.filter;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.result.interfaces.FilterResponseDataInterface;
import com.sojson.util.StringUtil;
import com.sojson.util.filter.FilterUtil;

/**
 * 过滤器返回数据信息工具类接口(页面跳转并保存之前访问的路径)
 * 
 * @author liu
 * @date 2020-11-02
 */
public class FilterResponseDataBySaveUrlAndRedirect implements FilterResponseDataInterface {

    private static FilterResponseDataBySaveUrlAndRedirect filterResponseDataBySaveUrlAndRedirect;

    private FilterResponseDataBySaveUrlAndRedirect() {}

    /**
     * 过滤器保存要访问的地址并重定向到指定页面
     * 
     * @throws IOException
     */
    @Override
    public void responseDate(ServletRequest request, ServletResponse response, TransErrorCode code, String url)
        throws IOException {
        saveUrlAndRedirect(request, response, url);
    }

    /**
     * 判断一个对象是否是空(空对象)
     * 
     * @param obj
     * @return
     */
    private static boolean isBlankObject(Object obj) {
        return StringUtil.isBlankObject(obj);
    }

    /**
     * 保存要访问的地址并重定向到指定页面
     * 
     * @param request
     * @param response
     * @param url 要重定向的地址
     * @throws IOException
     */
    private void saveUrlAndRedirect(ServletRequest request, ServletResponse response, String url) throws IOException {
        FilterUtil.saveUrlAndRedirect(request, response, url);
    }

    public static FilterResponseDataBySaveUrlAndRedirect getInstances() {
        if (isBlankObject(filterResponseDataBySaveUrlAndRedirect)) {
            filterResponseDataBySaveUrlAndRedirect = new FilterResponseDataBySaveUrlAndRedirect();
        }
        return filterResponseDataBySaveUrlAndRedirect;
    }

}